<!-- 组件说明 -->
<template>
  <div class='goods'>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb> 
     <!-- 卡片视图 -->
    <el-card class="box-card">
      <el-row :gutter="20">
        <!-- 搜索框内容 -->
        <el-col :span="8">
          <el-input clearable placeholder="请输入内容" @clear="clearInput" v-model="queryInfo.query" class="input-with-select">
            <el-button slot="append" @click="searchInfo" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
        <!-- 添加商品按钮  -->
        <el-col :span="4">
          <el-button type="primary" @click="goAddPage">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 商品列表区域 -->
      <el-table :data="goodsList" border stripe style="width: 100%">
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="95px"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="95px"></el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="140px">
          <template slot-scope="scope">
            {{scope.row.add_time | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="130px">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" icon="el-icon-edit"></el-button>
            <el-button size="mini" type="danger" @click="deleteGood(scope.row)" icon="el-icon-delete"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum" :page-sizes="[5, 10, 15, 20]" :page-size="queryInfo.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
  //import x from ''
  export default {
    components: {

    },
    data () {
      return {
        //绑定搜索区域内容
        queryInfo: {
          //当前页数
          pagenum: 1,
          //每页条数
          pagesize: 10,
          //查询内容
          query: ''
        },
        //商品列表内容数据
        goodsList: [],
        //列表数据总条数
        total: 0,
        
      };
    },
    created() {
      this.getGoodsList()
    },
    computed: {

    },
    methods: {
      // 获取商品列表信息
      async getGoodsList(){
        const{data:res} = await this.$http.get("goods",{params: this.queryInfo})
        if(res.meta.status !== 200) {
          return this.$message.error(res.meta.msg)
        }else{
          this.goodsList = res.data.goods
          console.log(this.goodsList);
          this.total = res.data.total
          // this.$message.success("获取商品列表成功")
        }
      },
      //监听分页组件中 每页条数改变
      handleSizeChange(newSize){
        this.queryInfo.pagesize = newSize
        this.getGoodsList()
      },
      //监听分页组件中 当前页数的改变
      handleCurrentChange(newNum){
        this.queryInfo.pagenum = newNum
        this.getGoodsList()
      },
      //监听清空输入框事件
      clearInput(){
        this.queryInfo.query = ''
        this.getGoodsList()
      },
      //监听 点击搜索按钮事件
      searchInfo(){
        this.getGoodsList()
      },
      //删除商品
      async deleteGood(goods){
        const str = await this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(err => err)
        if(str !== 'confirm'){
          return this.$message.info("已取消删除")
        }else{
          const{data:res} = await this.$http.delete(`goods/${goods.goods_id}`)
          if(res.meta.status !== 200){
            return this.$message.error(res.meta.msg)
          }else{
            this.$message.success("删除成功")
            this.getGoodsList()
          }
        }
      },
      //监听添加按钮事件
      goAddPage(){
        this.$router.push('/goods/add')
      }
    },
  }
</script>

<style lang='less' scoped>
//@import url()

</style>